package org.codeart.driver.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.codeart.common.dto.DriverUserWorkStatus;
import org.codeart.common.util.R;
import org.codeart.driver.mapper.DriverUserWorkStatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DriverUserWorkStatusService {

    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    /**
     * @param driverId
     * @param workStatus
     * @return
     */
    public boolean changeWorkStatus(Long driverId, Integer workStatus) {
        UpdateWrapper<DriverUserWorkStatus> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
               .set(workStatus != null, DriverUserWorkStatus::getWorkStatus, workStatus)
               .eq(DriverUserWorkStatus::getDriverId, driverId);
        int i = driverUserWorkStatusMapper.update(null, wrapper);
        return i != -1;
    }

    public R<DriverUserWorkStatus> getWorkStatus(Long driverId) {
        QueryWrapper<DriverUserWorkStatus> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DriverUserWorkStatus::getDriverId, driverId);
        DriverUserWorkStatus one = driverUserWorkStatusMapper.selectOne(wrapper);
        if (one != null) {
            return R.ok(one);
        }
        return R.fail();
    }
}
